import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

class ColorArea extends JPanel {
	// public static void main(String[] args) {
	// ColorArea ca = new ColorArea();
	// ca.restart();
	// }

	// five color sub areas
	SubArea sa1, sa2, sa3, sa4, sa5;
	List<SubArea> subareas;
	// Color corlorCombinations[] = new Color[5];
	static Random rand = new Random();
	Gene gene;
	static int numSubArea = 5;
	int count;// how many times this ColorArea has been selected.
	// the bigger count is, the more important this area in the evolution.
	boolean selected_last_time = false;
	boolean selected = false;
	boolean selected_again = false;

	static Color randColorGenerator() {
		int red = rand.nextInt(256);
		int green = rand.nextInt(256);
		int blue = rand.nextInt(256);
		return new Color(red, green, blue);

	}

	// clear all colors
	void clear() {
		sa1.setColor(Color.white);
		sa2.setColor(Color.white);
		sa3.setColor(Color.white);
		sa4.setColor(Color.white);
		sa5.setColor(Color.white);

		gene.update_gene();
	}

	public ColorArea() {
		count = 0;
		subareas = new ArrayList<SubArea>();
		// TODO Auto-generated constructor stub
		// this.setBackground(Color.white);
		// this.setLayout(new GridLayout(1, 5));
		this.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
		// this.setBorder(BorderFactory.createRaisedBevelBorder());
		this.setPreferredSize(new Dimension(228, 158));

		sa1 = new SubArea();
		// sa1.setSize(new Dimension(80, 150));
		sa1.setPreferredSize(new Dimension(80, 150));
		// sa1.setBackground(this.randColorGenerator());
		this.add(sa1);

		sa2 = new SubArea();
		// sa2.setSize(new Dimension(20, 100));
		sa2.setPreferredSize(new Dimension(60, 150));
		// sa2.setBackground(this.randColorGenerator());
		this.add(sa2);

		sa3 = new SubArea();
		sa3.setPreferredSize(new Dimension(40, 150));
		// sa3.setBackground(this.randColorGenerator());
		this.add(sa3);

		sa4 = new SubArea();
		sa4.setPreferredSize(new Dimension(20, 150));
		// sa4.setBackground(this.randColorGenerator());
		this.add(sa4);

		sa5 = new SubArea();
		sa5.setPreferredSize(new Dimension(20, 150));
		// sa5.setBackground(this.randColorGenerator());
		this.add(sa5);

		this.subareas.add(sa1);
		this.subareas.add(sa2);
		this.subareas.add(sa3);
		this.subareas.add(sa4);
		this.subareas.add(sa5);
		gene = new Gene(this);

		// this.gene.show_gene();
	}

	// restart function, randomly reset all sub areas' color;
	void restart() {
		sa1.reset();
		sa2.reset();
		sa3.reset();
		sa4.reset();
		sa5.reset();
		gene.update_gene();
		// gene.show_gene();

	}

	// the bit string should has the length of 24 (3 * 8)
	Color change_bitString_to_color(int[] short_gene) {
		if (short_gene.length != 24) {
			System.out.println("Please input a array with length 24");
			return null;
		} else {
			// System.out.println("the bit string is " + short_gene);
			int r = 0, g = 0, b = 0;
			int r_index = 0;
			int g_index = 8;
			int b_index = 16;
			for (int i = 7; i >= 0; i--) {
				r += short_gene[r_index++] * (int) Math.pow(2, i);
				g += short_gene[g_index++] * (int) Math.pow(2, i);
				b += short_gene[b_index++] * (int) Math.pow(2, i);

			}
			// System.out.format("r = %d, g = %d, b = %d \n", r, g, b);
			Color cl = new Color(r, g, b);
			// System.out.println("I change it to color " + cl);
			return cl;

		}
	}

	// the gene has been changed, update the color correspondingly
	void update_color() {
		int head = 0;
		int tail = 23;
		for (SubArea sa : this.subareas) {

			int[] short_gene = Gene.getSubArray(head, tail, this.gene.gene);
			Color cl = this.change_bitString_to_color(short_gene);
			sa.setColor(cl);

			head = tail + 1;
			tail = head + 23;
		}
		// this.gene.show_gene();

	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String temp = "";
		for (SubArea sa : this.subareas) {
			temp += sa.color;
			temp += "\n";
		}

		return temp;
	}

}
